*/
private static $instance;
+ /**
+ * Overrides the default instance of this class
+ * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
+ *
+ * If this method is used it MUST also be called with null after a test to ensure a new
+ * default instance is created next time getInstance is called.
+ *
+ * @since 1.27
+ *
+ * @param PageProps|null $store
+ *
+ * @return ScopedCallback to reset the overridden value
+ * @throws MWException
+ */
+ public static function overrideInstance( PageProps $store = null ) {
+ if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
+ throw new MWException(
+ 'Cannot override ' . __CLASS__ . 'default instance in operation.'
+ );
+ }
+ $previousValue = self::$instance;
+ self::$instance = $store;
+ return new ScopedCallback( function() use ( $previousValue ) {
+ self::$instance = $previousValue;
+ } );
+ }
+
/**
* @return PageProps
*/
'rc_logid' => 0,
'rc_log_type' => null,
'rc_log_action' => '',
- 'rc_params' => ''
+ 'rc_params' => serialize( [
+ 'hidden-cat' => WikiCategoryPage::factory( $categoryTitle )->isHidden()
+ ] )
];
$rc->mExtra = [
return $rc;
}
+ /**
+ * Get a parameter value
+ *
+ * @since 1.27
+ *
+ * @param string $name parameter name
+ * @return mixed
+ */
+ public function getParam( $name ) {
+ $params = $this->parseParams();
+ return isset( $params[$name] ) ? $params[$name] : null;
+ }
+
/**
* Initialises the members of this object from a mysql row object
*
}
return false;
}
+
+ /**
+ * Checks if a category is hidden.
+ *
+ * @since 1.27
+ *
+ * @return bool
+ */
+ public function isHidden() {
+ $pageId = $this->getTitle()->getArticleID();
+ $pageProps = PageProps::getInstance()->getProperties( $this->getTitle(), 'hiddencat' );
+
+ return isset( $pageProps[$pageId] ) ? true : false;
+ }
}
* @param Title $title
*
* @throws MWException
- * @return WikiPage Object of the appropriate type
+ * @return WikiPage|WikiCategoryPage|WikiFilePage
*/
public static function factory( Title $title ) {
$ns = $title->getNamespace();
$list = ChangesList::newFromContext( $this->getContext() );
$list->initChangesListRows( $rows );
+ $userShowHiddenCats = $this->getUser()->getBoolOption( 'showhiddencats' );
$rclistOutput = $list->beginRecentChangesList();
foreach ( $rows as $obj ) {
if ( $limit == 0 ) {
break;
}
$rc = RecentChange::newFromRow( $obj );
+
+ # Skip CatWatch entries for hidden cats based on user preference
+ if (
+ $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE &&
+ !$userShowHiddenCats &&
+ $rc->getParam( 'hidden-cat' )
+ ) {
+ continue;
+ }
+
$rc->counter = $counter++;
# Check if the page has been updated since the last visit
if ( $this->getConfig()->get( 'ShowUpdatedMarker' )
}
$s = $list->beginRecentChangesList();
+ $userShowHiddenCats = $this->getUser()->getBoolOption( 'showhiddencats' );
$counter = 1;
foreach ( $rows as $obj ) {
# Make RC entry
$rc = RecentChange::newFromRow( $obj );
+
+ # Skip CatWatch entries for hidden cats based on user preference
+ if (
+ $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE &&
+ !$userShowHiddenCats &&
+ $rc->getParam( 'hidden-cat' )
+ ) {
+ continue;
+ }
+
$rc->counter = $counter++;
if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
$this->assertEquals( $rcType, RecentChange::parseToRCType( $type ) );
}
+ /**
+ * @return PHPUnit_Framework_MockObject_MockObject|PageProps
+ */
+ private function getMockPageProps() {
+ return $this->getMockBuilder( PageProps::class )
+ ->disableOriginalConstructor()
+ ->getMock();
+ }
+
+ public function provideCategoryContent() {
+ return [
+ [ true ],
+ [ false ],
+ ];
+ }
+
+ /**
+ * @dataProvider provideCategoryContent
+ * @covers RecentChange::newForCategorization
+ */
+ public function testHiddenCategoryChange( $isHidden ) {
+ $categoryTitle = Title::newFromText( 'CategoryPage', NS_CATEGORY );
+
+ $pageProps = $this->getMockPageProps();
+ $pageProps->expects( $this->once() )
+ ->method( 'getProperties' )
+ ->with( $categoryTitle, 'hiddencat' )
+ ->will( $this->returnValue( $isHidden ? [ $categoryTitle->getArticleID() => '' ] : [ ] ) );
+
+ $scopedOverride = PageProps::overrideInstance( $pageProps );
+
+ $rc = RecentChange::newForCategorization(
+ '0',
+ $categoryTitle,
+ $this->user,
+ $this->user_comment,
+ $this->title,
+ $categoryTitle->getLatestRevID(),
+ $categoryTitle->getLatestRevID(),
+ '0',
+ false
+ );
+
+ $this->assertEquals( $isHidden, $rc->getParam( 'hidden-cat' ) );
+
+ ScopedCallback::consume( $scopedOverride );
+ }
}
--- /dev/null
+<?php
+
+class WikiCategoryPageTest extends MediaWikiLangTestCase {
+
+ /**
+ * @return PHPUnit_Framework_MockObject_MockObject|PageProps
+ */
+ private function getMockPageProps() {
+ return $this->getMockBuilder( PageProps::class )
+ ->disableOriginalConstructor()
+ ->getMock();
+ }
+
+ /**
+ * @covers WikiCategoryPage::isHidden
+ */
+ public function testHiddenCategory_PropertyNotSet() {
+ $title = Title::makeTitle( NS_CATEGORY, 'CategoryPage' );
+ $categoryPage = WikiCategoryPage::factory( $title );
+
+ $pageProps = $this->getMockPageProps();
+ $pageProps->expects( $this->once() )
+ ->method( 'getProperties' )
+ ->with( $title, 'hiddencat' )
+ ->will( $this->returnValue( [ ] ) );
+
+ $scopedOverride = PageProps::overrideInstance( $pageProps );
+
+ $this->assertFalse( $categoryPage->isHidden() );
+
+ ScopedCallback::consume( $scopedOverride );
+ }
+
+ public function provideCategoryContent() {
+ return [
+ [ true ],
+ [ false ],
+ ];
+ }
+
+ /**
+ * @dataProvider provideCategoryContent
+ * @covers WikiCategoryPage::isHidden
+ */
+ public function testHiddenCategory_PropertyIsSet( $isHidden ) {
+ $categoryTitle = Title::makeTitle( NS_CATEGORY, 'CategoryPage' );
+ $categoryPage = WikiCategoryPage::factory( $categoryTitle );
+
+ $pageProps = $this->getMockPageProps();
+ $pageProps->expects( $this->once() )
+ ->method( 'getProperties' )
+ ->with( $categoryTitle, 'hiddencat' )
+ ->will( $this->returnValue( $isHidden ? [ $categoryTitle->getArticleID() => '' ] : [ ] ) );
+
+ $scopedOverride = PageProps::overrideInstance( $pageProps );
+
+ $this->assertEquals( $isHidden, $categoryPage->isHidden() );
+
+ ScopedCallback::consume( $scopedOverride );
+ }
+}